package com.example.answer.androdi_db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.answer.R;

import android.app.Activity;
import android.database.Cursor;
import android.database.CursorWrapper;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;

public class MainActivity1 extends Activity {

	private DBManager1 mgr;
	private ListView listView;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main1);
		listView = (ListView) findViewById(R.id.listView);
		// ��ʼ��DBManager
		mgr = new DBManager1(this);
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		// Ӧ�õ����һ��Activity�ر�ʱӦ�ͷ�DB
		mgr.closeDB();
	}
	public void add(View view) {
		ArrayList<Person> persons = new ArrayList<Person>();
		Person person6 = new Person("1+1",6, "A：1；B:2；C：3；D：4；答案：A");
//		Person person1 = new Person("Java语言具有许多优点和特点，哪个反映了Java程序并行机制的特点？",1, "A：安全性；B:多线性；C：跨平台；D：可移植；答案：B");
//		Person person2 = new Person("若需要定义一个类域或者类方法，应该使用哪种修饰符？",2, "A：static；B:package；C：private；D：public；答案：A");
//		Person person3 = new Person("编译java文件源程序文件将生成相应的字节码文件，这些字节码文件的扩展名为?",3, "A：.byte；B:.class；C：.html；D：.exe；答案：B");
//		Person person4 = new Person("调用sleep()方法可以使一个线程停止运行",4, "A：对；B:错；答案：A");
//		Person person5 = new Person("下面关于Java语言中实例方法和类方法的说法，哪几项是对的？",5, "A：实例方法可以直接通过类名调用；B:类方法可以直接通过类名调用；C：实例方法可以操作实例变量也可以操作类变量；D：类方法可以操作实例变量也可以操作类变量；答案：BC");
//		persons.add(person1);
//		persons.add(person2);
//		persons.add(person3);
//		persons.add(person4);
//		persons.add(person5);
		persons.add(person6);

		mgr.add(persons);
	}
	public void update(View view) {
		Person person = new Person();
		person.name = "1+1";
		person.info = "题目：1+1；A：1；B:2；C：3；D：4；答案：B";
		mgr.updateAge(person);
	}
	public void delete(View view) {
		Person person = new Person();
		person.age = 6;
		mgr.deleteOldPerson(person);
	}
	public void query(View view) {
		List<Person> persons = mgr.query();
		ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
		for (Person person : persons) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("name", person.name);
			map.put("info", person.age+"." + person.info);
			list.add(map);
		}
		//ListView��Ҫһ��������
		SimpleAdapter adapter = new SimpleAdapter(this, list, android.R.layout.simple_list_item_2,
				new String[] { "name", "info" }, new int[] { android.R.id.text1, android.R.id.text2 });
		listView.setAdapter(adapter);
	}

	public void queryTheCursor(View view) {
		Cursor c = mgr.queryTheCursor();
		startManagingCursor(c); // �и���activity�����Լ�����������ȥ����Cursor����������
		CursorWrapper cursorWrapper = new CursorWrapper(c) {
			@Override
			public String getString(int columnIndex) {
				// �����ǰ��������
				if (getColumnName(columnIndex).equals("info")) {
					int age = getInt(getColumnIndex("age"));
					return age + " years old, " + super.getString(columnIndex);
				}
				return super.getString(columnIndex);
			}
		};
		// ȷ����ѯ�������"_id"��
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursorWrapper,
				new String[] { "name", "info" }, new int[] { android.R.id.text1, android.R.id.text2 });
		ListView listView = (ListView) findViewById(R.id.listView);
		listView.setAdapter(adapter);
	}

}
